import json
import base64
from sqlalchemy import Column, Integer, String, ForeignKey, Date, BigInteger, LargeBinary, Text
from models import BaseModels


# 设备保养记录表
class Upkeep(BaseModels):
    __tablename__ = 'upkeep'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    facility_id = Column(BigInteger, ForeignKey("facility.id", ondelete="SET NULL"), comment="设备id")
    operate = Column(Date, nullable=False, comment="这次保养日期")
    note = Column(Text, nullable=True, comment="保养记录日志")
    image = Column(LargeBinary, nullable=True, comment="保养拍照保存照片")
    data = Column(Text, nullable=True, comment="下次设备保养内容")
    remind = Column(Date, nullable=False, comment="下次保养日期")
    user_id = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), comment="谁保养的")

    def __repr__(self):
        return json.dumps({
            "repair_id": self.id,
            "facility_id": self.facility_id,
            "operate": self.operate.strftime("%Y-%m-%d"),
            "note": self.note,
            "image": base64.b64encode(self.image).decode('utf-8') if self.image else None,
            "remind": self.remind.strftime("%Y-%m-%d"),
            "user_id": self.user_id
        })
